package Dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import Bean.BeanOrder;
import Bean.BeanStudent;
import exception.BooksNotFoundException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import Bean.BeanBook;
import Bean.BeanOrderDetail;
import exception.BookNotFoundException;

public class OrderDetailDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public Collection<BeanOrderDetail> getOrderDetails(int orderId) throws BookNotFoundException {
		ArrayList<BeanOrderDetail> orderDetails = new ArrayList<>();
		try {
			Set<Integer>bookids=new HashSet<>();
			String selectStatement = "select num,book_id from order_details where a.order_id = ?";
			this.jdbcTemplate.query(selectStatement, new Object[] { orderId }, new ResultSetExtractor<BeanOrderDetail>() {
				@Override
				public BeanOrderDetail extractData(ResultSet rs) throws SQLException, DataAccessException {
					// TODO Auto-generated method stub
					while (rs.next()) {
						BeanOrderDetail orderDetail = new BeanOrderDetail();
						orderDetail.setOrder_id(orderId);
						orderDetails.add(orderDetail);
						bookids.add(rs.getInt(2));
					}
					return null;
				}
			});
			String strbookids="";
			for(Integer bookid:bookids) {
				if(strbookids==null)strbookids=bookid+"";
				else strbookids+=","+bookid;
			}
			selectStatement="select book_id,title,author,publisher,publishing_time,book_price,category_id,inventory,description from books where book_id in ("+strbookids+") order by id desc";

			Map<String, BeanBook>bookMap=new HashMap<>();
//			this.jdbcTemplate.query(selectStatement, new ResultSetExtractor<BeanBook>() {
//				@Override
//				public BeanBook extractData(ResultSet rs) throws SQLException, DataAccessException {
//					// TODO Auto-generated method stub
//					while(rs.next()) {
//						BeanBook bookDetails=new BeanBook();
//						bookDetails.setBook_id(rs.getString(1));
//						bookDetails.setTitle(rs.getString(2));
//						bookDetails.setAuthor(rs.getString(3));
//						bookDetails.setPublisher(rs.getString(4));
//						bookDetails.setPublishing_time(rs.getInt(5));
//						bookDetails.setBook_price(rs.getFloat(6));
//						bookDetails.setCategory_id(rs.getInt(7));
//						bookDetails.setInventory(rs.getInt(8));
//						bookDetails.setDescription(rs.getString(9));
//						bookMap.put(bookDetails.getBook_id(), bookDetails);
//					}
//					return null;
//				}
//			});
//			for(BeanOrderDetail d:orderDetails) {
//				d.setBookDetails(bookMap.get(d.getBook_id()));
//			}
		} finally {
			return orderDetails;
		}
	}
	public Collection<BeanOrderDetail> getStudentsByClass(int id) throws BooksNotFoundException {
		ArrayList<BeanOrderDetail>orderDetails= new ArrayList<>();
		String selectStatement = "select detail_id,order_id,student_id,isFormal,times,detail_price from `order_details` where class_id=?";
		this.jdbcTemplate.query(selectStatement, new ResultSetExtractor<BeanOrderDetail>() {
			@Override
			public BeanOrderDetail extractData(ResultSet rs) throws SQLException, DataAccessException {
				// TODO Auto-generated method stub
				while(rs.next()) {
					BeanOrderDetail orderDetail=new BeanOrderDetail();
					orderDetail.setDetail_id(rs.getInt(1));
					orderDetail.setOrder_id(rs.getInt(2));
					orderDetail.setStudent_id(rs.getInt(3));
					orderDetail.setFormal(rs.getBoolean(4));
					orderDetail.setTimes(rs.getInt(5));
					orderDetail.setDetail_price(rs.getFloat(6));
					orderDetails.add(orderDetail);
				}
				return null;
			}
		},id);
		for(BeanOrderDetail orderDetail:orderDetails){
			selectStatement="select student_name,student_age from student where student_id=?";
			this.jdbcTemplate.query(selectStatement, new ResultSetExtractor<BeanStudent>() {
				@Override
				public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
					// TODO Auto-generated method stub
					while(rs.next()) {
						BeanStudent student=new BeanStudent();
						student.setStudent_id(orderDetail.getStudent_id());
						student.setStudent_name(rs.getString(1));
						student.setStudent_age(rs.getInt(2));
						orderDetail.setStudent(student);
					}
					return null;
				}
			},orderDetail.getStudent_id());
		}
		return orderDetails;
	}
}
